package com.huluobo.a2205parkdemo.notice

import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2205parkdemo.R
import com.huluobo.a2205parkdemo.databinding.ActivityNoticeBinding
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class NoticeActivity : BaseActivity<ActivityNoticeBinding, NoticeViewModel>() {
    //    private lateinit var noticeAdapter: NoticeAdapter
    private lateinit var noticePagingAdapter: NoticePagingAdapter
    override fun initData() {
//        val map = mutableMapOf<String, Int>()
//        map[Const.PARAM_PAGE] = 0
//        val json = Gson().toJson(map)
//        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
//        vm.getAllNotice(body)

//        vm.getAllNoticeSuccess.observe(this) {
//            val list = Gson().fromJson(it, NoticeBean::class.java)
//            noticeAdapter.data.clear()
//            noticeAdapter.data.addAll(list)
//            noticeAdapter.notifyDataSetChanged()
//        }

        vm.addNoticeSuccess.observe(this) {
            ToastUtils.showLong("添加公告成功")
//            vm.getAllNotice(body)
        }
    }

    override fun initView() {
        binding.noticeHeader.headerTitle.text = "公告管理"
        binding.noticeHeader.headerBack.visibility = View.VISIBLE
        binding.noticeHeader.headerBack.setOnClickListener { finish() }
        binding.noticeHeader.headerAdd.visibility = View.VISIBLE
        binding.noticeHeader.headerAdd.setOnClickListener {
            val view = LayoutInflater.from(this).inflate(R.layout.dialog_add_notice, null)
            AlertDialog.Builder(this)
                .setView(view)
                .setTitle("添加新公告")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", object : OnClickListener {
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        val title = view.findViewById<EditText>(R.id.add_notice_title_et).text.toString()
                        val value = view.findViewById<EditText>(R.id.add_notice_value_et).text.toString()
                        if (title.isNotEmpty() && value.isNotEmpty()) {
                            val map = mutableMapOf<String, Any>()
                            map[Const.PARAM_NOTICE_TITLE] = title
                            map[Const.PARAM_NOTICE_VALUE] = value
                            map[Const.PARAM_USER_ID] = SPUtils.getInstance().getInt(Const.PARAM_U_ID)

                            val json = Gson().toJson(map)
                            val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                            vm.addNotice(body)
                        } else {
                            ToastUtils.showLong("公告标题或者内容不能为空")
                        }
                    }
                }).show()
        }

        binding.noticeRv.layoutManager = LinearLayoutManager(this)
//        noticeAdapter = NoticeAdapter()
//        binding.noticeRv.adapter = noticeAdapter
        noticePagingAdapter = NoticePagingAdapter()
        binding.noticeRv.adapter = noticePagingAdapter

        lifecycleScope.launch {
            vm.getNoticePagingData().collect {
                noticePagingAdapter.submitData(it)
            }
        }


    }
}